//
//  BaseTabBarController.m
//  GameBaby
//
//  Created by keyZhang on 13-12-19.
//  Copyright (c) 2013年 keyZhang. All rights reserved.
//

#import "BaseTabBarController.h"
#import "BaseNavigationController.h"
#import "HomeViewController.h"
#import "OpenServiceViewController.h"
#import "RankViewController.h"
#import "GiftPackViewController.h"
#import "SettingViewController.h"
#import "BootPictureView.h"
@interface BaseTabBarController ()

@end

@implementation BaseTabBarController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self initTabBarController];
    [self initTabbarView];
    
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]){
        BootPictureView *bootPIc = [[BootPictureView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        [self.view addSubview:bootPIc];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBar.hidden = YES;
}
/**
 *  初始化标签控制器
 */
- (void)initTabBarController
{
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    OpenServiceViewController *openServiceVC = [[OpenServiceViewController alloc] init];
    RankViewController *rankVC = [[RankViewController alloc] init];
    GiftPackViewController *giftPackVC = [[GiftPackViewController alloc] init];
    SettingViewController *settingVC = [[SettingViewController alloc] init];
    
    NSArray *viewCtrls = @[homeVC,rankVC,giftPackVC,openServiceVC,settingVC];
    NSMutableArray *baseNavCtrls = [NSMutableArray arrayWithCapacity:viewCtrls.count];

    
    for (int i = 0; i < viewCtrls.count; i++) {
        BaseNavigationController *baseNav = [[BaseNavigationController alloc]initWithRootViewController:viewCtrls[i]];
        baseNav.delegate = self;
        [baseNavCtrls addObject:baseNav];
    }
    
    self.viewControllers = baseNavCtrls;
    
}
/**
 *  初始化自定义标签视图
 */
- (void)initTabbarView;
{
    float itemWidth = kScreenWidth/5;
    
    //背景图片
    _tabbarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kScreenHeight-49-64, kScreenWidth, 49)];
    _tabbarView.image = [UIImage imageNamed:@"nav-d 2.png"];
    _tabbarView.userInteractionEnabled = YES;
//    [self.view addSubview:_tabbarView];
    
    //5个item
//    NSArray *imgArr = @[@"大厅bar.png", @"限免bar.png", @"抽奖bar.png", @"攻略bar.png", @"分类bar.png"];
    
    for (int i=0; i<5; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btn.frame = CGRectMake(i*itemWidth, 0, itemWidth, 50);
//        [btn setBackgroundImage:[UIImage imageNamed:imgArr[i]] forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor clearColor];
//        [btn setBackgroundImage:[UIImage imageNamed:imgArr[i]] forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(selectTabbar:) forControlEvents:UIControlEventTouchUpInside];
        [_tabbarView addSubview:btn];
    }
    //选中的图片
    _selectImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, itemWidth, 50)];
    _selectImgView.image = [UIImage imageNamed:@"nav-a1"];
    [_tabbarView addSubview:_selectImgView];
    
}


-(void)selectTabbar:(UIButton *)btn
{
    self.selectedIndex = btn.tag;
    NSArray *select_imgArr = @[@"nav-a1", @"nav-a_09", @"nav-a_10", @"nav-a_11", @"nav-a_12"];
    _selectImgView.image = [UIImage imageNamed:select_imgArr[btn.tag]];
    _selectImgView.center = btn.center;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UINavigationController delegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//    if (navigationController.viewControllers.count == 2)
//    {
//        //隐藏自定义tabbar
//        [self showTabBar:NO];
//    } else if (navigationController.viewControllers.count == 1)
//    {
//        //显示自定义tabbar
//        [self showTabBar:YES];
//    }
    
    if (navigationController.viewControllers.count == 1) {
        [viewController.view addSubview:_tabbarView];
    }
}

#pragma mark - showTabBar
//隐藏或显示自定义tabbar
- (void)showTabBar:(BOOL)show
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.35];
    if (show)
    {
        _tabbarView.left = 0;
    }else
    {
        _tabbarView.right = 0;
    }
    [UIView commitAnimations];
}

@end
